package com.hcrazy.manchester.app.controllers.member;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hcrazy.manchester.app.form.SearchForm;
import com.hcrazy.manchester.app.pojo.Feedback;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.FeedbackService;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/more")
public class FeedbackController {
	
	@Autowired
	private FeedbackService feedbackService;
	
	/**
	 * 查看信息内容
	 * @param infoId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/viewFeedback",method=RequestMethod.GET)
	public void viewFeedback(@RequestParam long feedbackId,Model model){
		try {
			//根据id获取信息内容
			Feedback fb = feedbackService.getById(feedbackId);
			//更新返回对象
			model.addAttribute("fb",fb);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/feedback",method=RequestMethod.GET)
	public void feedback(Model model,HttpServletRequest req){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllFeedbacks(1);
		model.addAttribute("pageModel", pageModel);
		
		model.addAttribute("searchForm",new SearchForm()); 
	}
	
	@RequestMapping(value="/feedback/search",method=RequestMethod.POST)
	public String searchFeedback(@ModelAttribute("form") SearchForm form,
			Model model,HttpServletRequest req){
		//是否执行删除操作
		if(form.getInfoId() > 0){
			try {
				feedbackService.delFeedback(form.getInfoId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		form.setInfoId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllFeedbacks(form.getPageNum());
		model.addAttribute("pageModel", pageModel);

		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/more/feedback";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllFeedbacks(int pageNum){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		
		try {
			return feedbackService.getPagedFeedbacks(model);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
}
